<?php

namespace App\Admin\Forms\User;

use App\Models\User\WxUser;
use App\Models\User\WxUserOnline;
use App\Models\WxAuthentication;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\Utils;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;

class UserNoticeForm extends Form implements LazyRenderable
{
    use LazyWidget;

    public function handle(array $input)
    {
        $action = $input['action'];
        if($action == 3){
            $action_3_title = $input['action_3_title'];
            $action_3_content = $input['action_3_content'];
            $action_3_target_type = $input['action_3_target_type'];
            $action_3_target_id = $input['action_3_target_id'];
            $action_3_online_force = $input['action_3_online_force'];
            $action_3_object = $input['action_3_object'];
            $receive_handle = 0;
            if(_empty_($action_3_title) || _empty_($action_3_content)){
                return $this->response()->error('标题和内容不能为空！');
            }
            if($action_3_online_force == 1){
                $receive_handle = 2;
            }
            if($action_3_object == 0){
                // 所有用户
                $res = Utils::app_push($action_3_title, $action_3_content, 999, ['target_type'=>$action_3_target_type, 'target_id'=>$action_3_target_id], 'all', [], $receive_handle, false);
                if($res['code'] == 0){
                    return $this->response()->success('推送成功');
                }else{
                    return $this->response()->error($res['msg']);
                }
            }else if($action_3_object == 1){
                // 在线的用户
                $onlines = WxUserOnline::where('online', '>', now()->subMinute(3))->orderBy('online', 'desc')->get();
                if($onlines->isEmpty()){
                    return $this->response()->error('一个在线用户也没有！');
                }
                $online_ids = [];
                if($onlines) {
                    $onlines->map(function ($item) use (&$online_ids) {
                        $online_ids[] = $item->user_id;
                    });
                }
                $res = Utils::app_push($action_3_title, $action_3_content, 999, ['target_type'=>$action_3_target_type, 'target_id'=>$action_3_target_id], 'batch', $online_ids, $receive_handle, false);
                if($res['code'] == 0){
                    return $this->response()->success('推送成功');
                }else{
                    return $this->response()->error($res['msg']);
                }
            }else if($action_3_object == 2){
                // 认证用户
                $users = WxAuthentication::where('authentication_state', 1)->get();
                if($users->isEmpty()){
                    return $this->response()->error('一个认证用户也没有！');
                }
                $online_ids = [];
                if($users) {
                    $users->map(function ($item) use (&$online_ids) {
                        $online_ids[] = $item->user_id;
                    });
                }
                $res = Utils::app_push($action_3_title, $action_3_content, 999, ['target_type'=>$action_3_target_type, 'target_id'=>$action_3_target_id], 'batch', $online_ids, $receive_handle, false);
                if($res['code'] == 0){
                    return $this->response()->success('推送成功');
                }else{
                    return $this->response()->error($res['msg']);
                }

            }else if($action_3_object == 3){
                // 会员用户
                $users = WxUser::where('is_member','>=', (string)time())->get();
                if($users->isEmpty()){
                    return $this->response()->error('一个会员也没有！');
                }
                $online_ids = [];
                if($users) {
                    $users->map(function ($item) use (&$online_ids) {
                        $online_ids[] = $item->id;
                    });
                }
                $res = Utils::app_push($action_3_title, $action_3_content, 999, ['target_type'=>$action_3_target_type, 'target_id'=>$action_3_target_id], 'batch', $online_ids, $receive_handle, false);
                if($res['code'] == 0){
                    return $this->response()->success('推送成功');
                }else{
                    return $this->response()->error($res['msg']);
                }

            }else if($action_3_object == 4){
                // 特定用户
                $action_3_user_id = $input['action_3_user_id'];
                if(_empty_($action_3_user_id)){
                    return $this->response()->error('你还没有选择接收的用户！');
                }
                $res = Utils::app_push($action_3_title, $action_3_content, 999, ['target_type'=>$action_3_target_type, 'target_id'=>$action_3_target_id], 'batch', $action_3_user_id, $receive_handle, false);
                if($res['code'] == 0){
                    return $this->response()->success('推送成功');
                }else{
                    return $this->response()->error($res['msg']);
                }
            }


        }else if($action == 4){
            global $__MINI_GLOBAL_PAYCODE_2__;
            if(!$__MINI_GLOBAL_PAYCODE_2__){
                return $this->response()->error('你还没有解锁 付费2！');
            }
            $action_4_title = $input['action_4_title'];
            $action_4_content = $input['action_4_content'];
            $action_4_object = $input['action_4_object'];
            $action_4_online_force = _empty_default_($input['action_4_online_force'], false);
            if($action_4_object == 4){
                $action_4_user_id = $input['action_4_user_id'];
                if(_empty_($action_4_user_id)){
                    return $this->response()->error('你还没有选择接收的用户！');
                }
                if(is_array($action_4_user_id)){
                    foreach ($action_4_user_id as $uid){
                        Utils::templateMessage($uid, $action_4_title, $action_4_content, '', '', '', 'notice', $action_4_online_force);
                    }
                }
                return $this->response()->success('推送成功');
            }else if($action_4_object == 5){
                $user_ids = WxUser::whereNotNull('weixin_mp_openid')->pluck('id')->toArray();
                if(_empty_($user_ids)){
                    return $this->response()->warning('一个绑定公众号的用户也没有');
                }
                foreach ($user_ids as $uid){
                    Utils::templateMessage($uid, $action_4_title, $action_4_content, '', '', '', 'notice', $action_4_online_force);
                }
                return $this->response()->success('推送成功');
            }
        }
    }

    public function form()
    {
        // [0=>'私信通知', 1=>'系统通知', 2 => '首页弹窗', 3 =>'App推送', 4 => '公众号模板消息']
        $this->radio('action', '通知类型')->required()->options([3 => 'App推送', 4 => '公众号模板消息'])->when(3, function (Form $form){
            $this->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">说明：1、针对ios，你需要app上线，然后在dcloud配置推送证书<br/>2、针对安卓，你需要配置离线推送的各个厂商账号</div>';
            }, '');
            $form->radio('action_3_object', '通知对象')->options(FieldUtils::selectUsers())->required()->when(4, function (Form $form){
                $form->multipleSelect('action_3_user_id', '接收的用户')->options(function ($id) {
                    if($id){
                        $user = \App\Models\User\WxUser::find($id);
                        if ($user) {
                            return [$user->id => $user->user_name];
                        }
                    }
                })->ajax('select/users');
            })->default(0);
            $form->text('action_3_title', '标题 *');
            $form->textarea('action_3_content', '内容 *');
            $form->radio('action_3_target_type', '跳转类型')->options(FieldUtils::getUrlTypes())->default(0)->required();
            $form->text('action_3_target_id', 'target_id')->help('<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
            $form->switch('action_3_online_force', '在线强制提醒')->help('开启强制之后，正在使用App的人也会获得通知提醒')->default(1);
        })->when(4, function (Form $form){
            $form->radio('action_4_object', '通知对象')->options([4 => '指定用户', 5=>'所有用户'])->required()->when(4, function (Form $form){
                $form->multipleSelect('action_4_user_id', '接收的用户')->options(function ($id) {
                    if($id){
                        $user = \App\Models\User\WxUser::find($id);
                        if ($user) {
                            return [$user->id => $user->user_name];
                        }
                    }
                })->ajax('select/users');
            })->default(4);
            $form->text('action_4_title', '标题 *');
            $form->textarea('action_4_content', '内容 *');
            $form->switch('action_4_online_force', '在线强制提醒');
        })->default(3);
    }
}
